#	dir config 
RTL_DIR = ../../../../../design/rtl
COMMONIP_DIR = ../../../../../design/common_ip
TB_TOP = ../../../uvm_tb/test.sv
MII_IF = ../../../uvm_tb/mii_if.sv
UVM_HOME = ~/uvm-1.1_lab/uvm-1.1a

INC_DIR = +incdir+../../../uvm_tb
INC_DIR += +incdir+${COMMONIP_DIR}
INC_DIR += +incdir+../../../uvm_tb/testcases

#	sim option
TESTNAME ?= mac_single_port_polling_smoke
VERBOSITY ?= UVM_LOW
UVM_VER = uvm-1.1
SOLVER = 2
SEED = 1
FSDB_FLAG = 1
SIM_OPT += +UVM_VERBOSITY=$(VERBOSITY)
SIM_OPT += +UVM+TIMEOUT="1000000000ns,YES" #watch dog time out
SIM_OPT += +UVM_TR_RECORD 
SIM_OPT += +UVM_LOG_RECORD
SIM_OPT += +UVM_MAX_QUIT_COUNT=1

#SIM_OPT += +ntb_random_seed_automatic
#SIM_OPT += +UVM_OBJECTION_TRACE

#	cov option
COV_TOOL = verdi
COVER ?= 0
ifeq (${COVER},1)
COV_COMP_OPT = -cm line+cond+fsm+branch+tgl -cm_dir ../../coverage/sim_${TESTNAME}_seed_$(SEED).vdb -cm_hier ./cov.cfg +define+COV_EN
COV_SIM_OPT = -cm line+cond+fsm+branch+tgl -cm_dir ../../coverage/sim_${TESTNAME}_seed_$(SEED).vdb -cm_name ${TESTNAME}
else
COV_COMP_OPT = 
COV_SIM_OPT = 
endif

ifeq (${FSDB_FLAG},1)
DEFINE_OPT = +define+FSDB_FLAG=$(FSDB_FLAG) 
else
DEFINE_OPT = 
endif

all: precompile mklist compile sim  	
regression: precompile compile sim
# *.sv $(DUT) $(HARNESS_TOP) $(TB_TOP)
mklist_r:
	[ -d test_dir ] || mkdir -m 777 test_dir
	cd test_dir && \
	[ -d sim_dir ] || mkdir -m 777 sim_dir
	cd test_dir && \
	cd sim_dir &&\
	rm -rf ${RTL_DIR}/filelist.f && \
	touch ${RTL_DIR}/filelist.f && \
	find ${COMMONIP_DIR} -type f >> ${RTL_DIR}/filelist.f && \
	find ${RTL_DIR}/*.v >> ${RTL_DIR}/filelist.f
	rm -rf test_dir

mklist:
	cd ${TESTNAME}_test_dir && \
	cd ${TESTNAME}_${SEED}_sim_dir &&\
	rm -rf ${RTL_DIR}/filelist.f && \
	touch ${RTL_DIR}/filelist.f && \
	find ${COMMONIP_DIR} -type f >> ${RTL_DIR}/filelist.f && \
	find ${RTL_DIR}/*.v >> ${RTL_DIR}/filelist.f

precompile:
	[ -d ${TESTNAME}_test_dir ] || mkdir -m 777 ${TESTNAME}_test_dir
	cd ${TESTNAME}_test_dir && \
	[ -d ${TESTNAME}_${SEED}_sim_dir ] || mkdir -m 777 ${TESTNAME}_${SEED}_sim_dir

compile:
	cd ${TESTNAME}_test_dir/${TESTNAME}_${SEED}_sim_dir  && \
	vcs -full64 -Mupdate ${DEFINE_OPT} -sverilog -debug_acc+all -ntb_opts ${UVM_VER} -lca -kdb -timescale="1ns/1ns" \
	${COV_COMP_OPT} ${INC_DIR} \
	-f ${RTL_DIR}/filelist.f \
	-l vcs_comp.log \
	-top switch_tb ${TB_TOP}	

sim:
	cd ${TESTNAME}_test_dir && \
	cd ${TESTNAME}_${SEED}_sim_dir &&\
	./simv ${SIM_OPT} ${COV_SIM_OPT} -l ${TESTNAME}_${SEED}_sim_dir/sim_${TESTNAME}.log \
	+TESTNAME=${TESTNAME} +UVM_TESTNAME=${TESTNAME} +ntb_solver_mode=$(SOLVER) +ntb_random_seed=$(SEED) 
dve:
	dve -vpd vcdplus.vpd&

verdi:
	cd ${TESTNAME}_test_dir && \
	cd ${TESTNAME}_${SEED}_sim_dir &&\
	verdi \
	-sv	\
	+v2k \
	-F ${RTL_DIR}/filelist.f	\
	-ssf ${TESTNAME}.fsdb -elab simv.daidir/kdb &

merge_cov:
	urg -full64 -flex_merge reference -format both -dir ./coverage/*.vdb -dbname merge.vdb

cov: merge_cov
	firefox urgReport/dashboard.html &
ifeq ($(COV_TOOL),verdi)
	verdi -cov -covdir merge.vdb/ &
else ifeq ($(COV_TOOL),dve)
	dve -full64 -cov -dir merge.vdb &
else ifeq ($(COV_TOOL),urg)
	urg -full64 -dir merge.vdb &
else
endif

.PHONY: clean cleanall
clean:
	rm -rf csrc simv simv.* *.fsdb .__solver_cache__
	rm -rf novas* ucli.key vc_hdrs.h

cleanall:clean
	rm -rf coverage merge.vdb urgReport vdCovLog
	rm -rf verdiLog *.log *_sim_dir
